/**
 * axios二次封装
 * **/
import axios from 'axios'
import {  Message } from 'element-ui'

// 创建axios实例对象，添加全局配置
const service = axios.create({
    baseURL: '/',
    timeout: 8000
})

// 请求拦截
service.interceptors.request.use(req => {
    //发送请求前的操作
    const headers = req.headers;
    const token = window.sessionStorage.getItem('token')
    if(token) headers.token = token;
    return req;
},function (error) {
    return Promise.reject(error);
})

// 响应拦截
service.interceptors.response.use(response => {
        const res = response.data;
        return res;
    },
error => {
    const res = error.response.data
    Message({
        message: res.msg || error.message,
        type: 'error',
        duration: 5 * 1000
    })
    return Promise.reject(res.msg || error.message)

})

export default service
